I'm creating a child component for my filters which should be updated when the user changes some input values... The problem is that when I emit the new data into the parent component the watch won't trigger! Please review my code to see where I went wrong...
<FiltersComponent v-model:filters="filters" />
watch(filters, (value) => {
getAccounts(value);
});
const emits = defineEmits(['update:filters']);
const props = defineProps({
filters: {
type: Object,
default: () => ({
...
}),
required: true,
},
});
function updateFilters(newValue, property) {
console.log('updateFilters', newValue, property);
emits('update:filters', { ...props.filters, [property]: newValue });
}
note: the console.log prints the newValue and property correctly but the watch never happens!
PS: I used the v-on:update:filters="getAccounts(filters)" inside parent component too. but the filters data is not updated inside function call...
You need to set deep to true when watching an array or object so that Vue knows that it should watch the nested data for changes. Try using this:
watch(filters, (value) => {
getAccounts(value);
},
{
deep: true
}
);
For reference please review the vue 3 documentation: https://v3.vuejs.org/guide/migration/watch.html#_3-x-syntax